package com.ruoyi.job.task;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.job.mapper.MaterialReportMapper;
import com.ruoyi.omp.api.MaterialReport;
import com.ruoyi.omp.api.domain.MaterialInOutRecord;
import com.ruoyi.omp.api.domain.MaterialInventory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Component("materialReportTask")
public class MaterialReportTask {

    @Autowired
    private MaterialReportMapper reportMapper;

    public void createMonthReport()  {

        Date newDate = DateUtil.offset(new Date(), DateField.MONTH, -1);

        Date lastDate = DateUtil.offset(new Date(), DateField.MONTH, -2);

        String month = DateUtil.format(newDate, "yyyy-MM");

        String lastMonth = DateUtil.format(lastDate, "yyyy-MM");

        // 查询物料出入库情况
        List<MaterialInOutRecord> list = reportMapper.selectInOutRecordByMonth(month);
        // 入库数量
        List<MaterialInOutRecord> inRecords = list.stream().filter(o -> o.getRecordType().equals("0")).collect(Collectors.toList());
        // 出库数量
        List<MaterialInOutRecord> outRecords = list.stream().filter(o -> o.getRecordType().equals("1")).collect(Collectors.toList());
        // 查询物料库存
        List<MaterialInventory> inventories = reportMapper.selectMaHouseMaterialList();
        List<MaterialReport> reports = new ArrayList<>();
        for (MaterialInventory inventory : inventories) {
            MaterialReport report = new MaterialReport();
            report.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
            report.setMaterialId(inventory.getMaterialId());
            report.setHouseId(inventory.getHouseId());
            report.setNowBalance(inventory.getQuantity());
            report.setMaterialCode(inventory.getMaterialName());
            report.setAddress(inventory.getHouseNum());
            report.setParkId(inventory.getParkId());
            try {
                report.setReportDate(new SimpleDateFormat("yyyy-MM-dd").parse(month + "-01"));
            }catch (Exception e){
                e.printStackTrace();
            }
            List<MaterialInOutRecord> inFilter = inRecords.stream().filter(o -> o.getMaterialId().equals(inventory.getMaterialId()) && o.getWarehouseId().equals(inventory.getHouseId())).collect(Collectors.toList());
            List<MaterialInOutRecord> outFilter = outRecords.stream().filter(o -> o.getMaterialId().equals(inventory.getMaterialId()) && o.getWarehouseId().equals(inventory.getHouseId())).collect(Collectors.toList());
            if (inFilter.size() > 0) {
                MaterialInOutRecord rd = inFilter.get(0);
                report.setInStorage(rd.getQuantity());
            }
            if (outFilter.size() > 0) {
                MaterialInOutRecord rd = outFilter.get(0);
                report.setOutStorage(rd.getQuantity());
            }
            reports.add(report);
        }
        // 查询物料上月结存
        List<MaterialReport> lastReports = reportMapper.selectReportByMonth(lastMonth + "-01");
        lastReports.forEach(o -> {
            for (MaterialReport report : lastReports) {
                if (o.getMaterialId().equals(report.getMaterialId()) && o.getHouseId().equals(report.getHouseId())){
                    o.setLastBalance(report.getNowBalance());
                }
            }
        });
        // 写入报表
        reportMapper.insertReportList(reports);

    }

}
